package com.purong.purong.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.purong.purong.R;
import com.purong.purong.adapter.AreaListAdapter;
import com.purong.purong.bean.AreaBean;
import com.purong.purong.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/22.
 */
public class AreaSelectDialog extends Dialog {
    private Context mContext;
    private AreaBean mProvince,mCity,mDistrict;
    private OnAreaClickListener mListener;
    private TextView provinceTv,cityTv,districtTv;
    private ImageView provinceBtm,cityBtm,districtBtm;
    private int status=0;//0,未选，1省，2市，3区
    private List<AreaBean> areaList;
    private AreaListAdapter mAdapter;
    private ListView mListView;
    public AreaSelectDialog(Context context, AreaBean province, AreaBean city, AreaBean district, OnAreaClickListener listener) {
        super(context, R.style.DialogTransparent);
        this.mContext=context;
        this.mProvince=province;
        this.mCity=city;
        this.mDistrict=district;
        this.mListener=listener;
        setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialogWindowAnim);
        if (district!=null){
            status=3;
        }else if (city!=null){
            status=2;
        }else if (province!=null){
            status=1;
        }else {
            status=0;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_address_select);
        findViewById(R.id.main_lay).setLayoutParams(new FrameLayout.LayoutParams(CommonUtil.getScreenWidth(mContext),CommonUtil.getScreenHeight(mContext)/5*3));
        findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        provinceTv=(TextView)findViewById(R.id.province_tv);
        cityTv=(TextView)findViewById(R.id.city_tv);
        districtTv=(TextView)findViewById(R.id.district_tv);
        provinceBtm=(ImageView)findViewById(R.id.province_bottom);
        cityBtm=(ImageView)findViewById(R.id.city_bottom);
        districtBtm=(ImageView)findViewById(R.id.district_bottom);
        mListView=(ListView)findViewById(R.id.list_view);
        areaList=new ArrayList<>();
        mAdapter=new AreaListAdapter(mContext,areaList);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                if (areaList.get(position).isCheck()){
//                    return;
//                }
                if (mListener!=null){
                    switch (status){
                        case 0:
                            status=1;
                            mCity=null;
                            mDistrict=null;
                            mProvince=areaList.get(position);
                            mListener.onLoadArea(areaList.get(position).getArea_id());
                            break;
                        case 1:
                            status=2;
                            mDistrict=null;
                            mCity=areaList.get(position);
                            mListener.onLoadArea(areaList.get(position).getArea_id());
                            break;
                        case 2:
                        case 3:
                            status=3;
                            mDistrict=areaList.get(position);
                            for (int i = 0; i <areaList.size() ; i++) {
                                if (i==position){
                                    areaList.get(i).setCheck(true);
                                }else {
                                    areaList.get(i).setCheck(false);
                                }
                            }

                            mAdapter.notifyDataSetChanged();
                            if (mListener!=null){
                                mListener.onResult(mProvince,mCity,mDistrict);
                            }
                            dismiss();
//                            Toast.makeText(mContext,mProvince.getAcronym()+mCity.getAcronym()+mDistrict.getAcronym(),Toast.LENGTH_LONG).show();
                            break;

                    }
                }
                initStatus(true);
            }
        });
        provinceTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mCity=null;
//                mDistrict=null;
                status=0;
                initStatus(false);
                if (mListener!=null){
                    mListener.onLoadArea("");
                }
            }
        });
        cityTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mDistrict=null;
                status=1;
                initStatus(false);
                if (mListener!=null){
                    mListener.onLoadArea(mProvince.getArea_id());
                }
            }
        });
        districtTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                status=2;
                initStatus(false);
                if (mListener!=null){
                    mListener.onLoadArea(mCity.getArea_id());
                }
            }
        });
        initData();
        if (status!=0){
            initStatus(false);
        }else {
            initStatus(true);
        }

    }
    public void setData(List<AreaBean> list){
        areaList.clear();
        if (list!=null){
            if (status==0&&mProvince!=null){
                for (AreaBean bean:list){
                    if (bean.getArea_id().equals(mProvince.getArea_id())){
                        bean.setCheck(true);
                        break;
                    }
                }
            }else if (status==1&&mCity!=null){
                for (AreaBean bean:list){
                    if (bean.getArea_id().equals(mCity.getArea_id())){
                        bean.setCheck(true);
                        break;
                    }
                }
            }
            else if ((status==2||status==3)&&mDistrict!=null){
                for (AreaBean bean:list){
                    if (bean.getArea_id().equals(mDistrict.getArea_id())){
                        bean.setCheck(true);
                        break;
                    }
                }
            }
            areaList.addAll(list);
        }

        mAdapter.notifyDataSetChanged();

//        mAdapter.notifyDataSetChanged();
    }
    public void initData(){
        if (status==3){
            if (mListener!=null){
                mListener.onLoadArea(mCity.getArea_id());
            }
        }else {
            if (mListener!=null){
                mListener.onLoadArea("");
            }
        }
    }
    public void initStatus(boolean isItemClick){
        provinceTv.setText( mProvince==null?"":mProvince.getAcronym());
        cityTv.setText( mCity==null?"":mCity.getAcronym());
        districtTv.setText( mDistrict==null?"":mDistrict.getAcronym());
        provinceTv.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
        cityTv.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
        districtTv.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
        provinceBtm.setVisibility(View.GONE);
        cityBtm.setVisibility(View.GONE);
        districtBtm.setVisibility(View.GONE);
       switch (status){
           case 0:
               if (mProvince==null&&isItemClick){
                   provinceTv.setText("请选择");
               }
               provinceBtm.setVisibility(View.VISIBLE);
               provinceTv.setTextColor(mContext.getResources().getColor(R.color.blue));
               break;
           case 1:
               if (mCity==null&&isItemClick){
                   cityTv.setText("请选择");
               }
               cityBtm.setVisibility(View.VISIBLE);
               cityTv.setTextColor(mContext.getResources().getColor(R.color.blue));
               break;
           case 2:
           case 3:
               if (mDistrict==null&&isItemClick){
                   districtTv.setText("请选择");
               }
               districtBtm.setVisibility(View.VISIBLE);
               districtTv.setTextColor(mContext.getResources().getColor(R.color.blue));
               break;
       }
    }
    public interface OnAreaClickListener{
        void onLoadArea(String parentId);
        void onResult(AreaBean province, AreaBean city, AreaBean distric);
    }
}
